package org.nf.web.servlet.view;

import org.nf.web.servlet.BaseView;
import org.nf.web.servlet.ServletApi;

import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @author wangl
 * @date 2021/5/28
 * 转发视图
 */
public class ForwardView extends BaseView {

    private final String url;

    private final Map<String, Object> map = new HashMap<>();

    public ForwardView(String url){
        this.url = url;
    }

    public void addObject(String name, Object obj) {
        map.put(name, obj);
    }

    @Override
    protected void render(ServletApi api) throws ServletException, IOException {
        HttpServletRequest request = api.request();
        HttpServletResponse response = api.response();
        map.forEach(request::setAttribute);
        request.getRequestDispatcher(url).forward(request, response);
    }
}
